package com.tiantian.app.activities;

import com.infindo.frame.util.ScreenManager;
import com.tiantian.app.R;
import com.tiantian.app.manager.DataModel;
import com.tiantian.app.manager.HttpManager;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
 /** 
 * @author  William.Chen
 * @version 创建时间：2013-3-10 上午8:41:47 
 * 类说明 
 */
public class PageTeacherHomeActivity extends SuperActivity {

	private final String TAG = PageTeacherHomeActivity.class.getSimpleName();
	
	private final int HANDLER_UI_TEACHER_ICON = 1;
	
	private RelativeLayout announceLayout;
	private RelativeLayout connectionLayout;
	
	private Drawable tearchIcon;
	private ImageView ivTeacherIcon;
	private TextView tvTeacherName,tvClassName,tvClassTitle;
	
	/* 
	 * @see com.tiantian.app.activities.SuperActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_teacherhome);
		onResetUI();
		
		ScreenManager.getScreenManager().popAllActivityAddMe(this);
	}
	/* 
	 * @see com.tiantian.app.activities.SuperActivity#callback(java.lang.String, int, java.lang.Object)
	 */
	@Override
	public void callback(String type, int position, Object obj) {
		// TODO Auto-generated method stub
        if("teacherIcon".equals(type)){
			if(obj instanceof byte[]){	
				byte[] b = (byte[]) obj;
				Bitmap tempBitmap =  BitmapFactory.decodeByteArray(b, 0, b.length);  
				if (tempBitmap != null ) {
					tearchIcon = new BitmapDrawable(tempBitmap);
					tempBitmap = null;
				}
			}
			handler.sendEmptyMessage(HANDLER_UI_TEACHER_ICON);
        }
	}
	
	
	final Handler handler = new Handler(){
		   public void handleMessage(Message msg) {
			   switch(msg.what){
			   case HANDLER_UI_TEACHER_ICON:
				   ivTeacherIcon.setBackgroundDrawable(tearchIcon);
				   ivTeacherIcon.invalidate();
				   break;
			   }
		   };
	};

	/* 
	 * @see com.tiantian.app.activities.SuperActivity#findView()
	 */
	@Override
	public void findView() {
		// TODO Auto-generated method stub
		superTitle = (TextView)findViewById(R.id.superTitle);
		superTitle.setText("首页");
		
	   menuHome = (Button)findViewById(R.id.menuHome);
	   menuAnnounce = (Button)findViewById(R.id.menuAnnounce);
	   menuConnection = (Button)findViewById(R.id.menuConnection);
	   menuMore = (Button)findViewById(R.id.menuMore);
	   
	   menuHome.setBackgroundResource(R.drawable.icon_select);
	   Drawable top = getResources().getDrawable(R.drawable.icon_home_b);
	   menuHome.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null);
	   
	   announceLayout = (RelativeLayout)findViewById(R.id.announceLayout);
	   connectionLayout = (RelativeLayout)findViewById(R.id.connectionLayout);
	   
	   ivTeacherIcon 	= 	(ImageView)findViewById(R.id.ivTeacherIcon);
	   tvTeacherName	= 	(TextView)findViewById(R.id.tvTeacherName);
	   tvClassName 		= 	(TextView)findViewById(R.id.tvClassName);
	   tvClassTitle 	= 	(TextView)findViewById(R.id.tvClassTitle);
	}

	/* 
	 * @see com.tiantian.app.activities.SuperActivity#initData()
	 */
	@Override
	public void initData() {
		// TODO Auto-generated method stub
		   if(DataModel.tempLoginResultTeacher==null)
			   return;
		   tvTeacherName.setText(""+DataModel.tempLoginResultTeacher.getUserObj().getTrueName());
		   tvClassName.setText(""+DataModel.tempLoginResultTeacher.getClassName());
		   tvClassTitle.setText(""+DataModel.tempLoginResultTeacher.getTitle());
		
		   HttpManager.reqestImage(this, DataModel.tempLoginResultTeacher.getUserObj().getAvatarPic(), 0, "teacherIcon", this);
	}

	/* 
	 * @see com.tiantian.app.activities.SuperActivity#onResetUI()
	 */
	@Override
	public void onResetUI() {
		// TODO Auto-generated method stub
        findView();
        setListener();
        initData();
	}

	/* 
	 * @see com.tiantian.app.activities.SuperActivity#setListener()
	 */
	@Override
	public void setListener() {
		// TODO Auto-generated method stub
		   menuAnnounce.setOnClickListener(this);
		   menuConnection.setOnClickListener(this);
		   menuMore.setOnClickListener(this);
		   
		   announceLayout.setOnClickListener(this);
		   connectionLayout.setOnClickListener(this);
	}
	
	/* 
	 * @see com.tiantian.app.activities.SuperActivity#onClick(android.view.View)
	 */
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		super.onClick(view);
		Intent it;
		switch(view.getId()){
		case R.id.announceLayout:
            it = new Intent(PageTeacherHomeActivity.this,PageAnnounceHomeActivity.class);
            startActivity(it);
			break;
		case R.id.connectionLayout:
			it = new Intent(PageTeacherHomeActivity.this,PageConnectionHomeActivity.class);
			startActivity(it);
			break;
		}
	}

}
